package com.mall.shopping.services;

import com.github.pagehelper.PageInfo;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ProductConverter;
import com.mall.shopping.converter.ProductDetailConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dto.*;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Description:
 * @Author raohui
 * @Date 2021/6/19 12:41
 */
@Service
public class IProductServiceImpl implements IProductService {

    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ProductConverter productConverter;
    @Autowired
    ProductDetailConverter productDetailConverter;

    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {

        ProductDetailResponse productDetailResponse = new ProductDetailResponse();
        try {
            //检验参数的是否合法
            request.requestCheck();
            Example example = new Example(Item.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("id",request.getId());
            Item item = itemMapper.selectByPrimaryKey(example);
            ProductDetailDto productDetailDto = productDetailConverter.item2Dto(item);
            productDetailResponse.setProductDetailDto(productDetailDto);
            productDetailResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            productDetailResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            return productDetailResponse;
        } catch (Exception e) {
            productDetailResponse.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
            productDetailResponse.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
        }
        return null;
    }



    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        AllProductResponse allProductResponse = new AllProductResponse();
        try {
            request.requestCheck();
            //分页插件，自动执行sql语句
            /*PageHelper.startPage(request.getPage(),request.getSize());
            Example example = new Example(Item.class);
            if (request.getSort() != null) {
                example.setOrderByClause(request.getSort());
            }
            Example.Criteria criteria = example.createCriteria();
            if (request.getPriceGt() != null) {
                criteria.andGreaterThanOrEqualTo("price",request.getPriceGt());
            }
            if (request.getPriceGt() != null) {
                criteria.andLessThanOrEqualTo("price",request.getPriceLte());
            }*/
            //查询

            //List<Item> items = itemMapper.selectByExample(example);
            if ("1".equals(request.getSort()) ) {
                selectItems(request, allProductResponse, "asc");
                return allProductResponse;
            }
            if ("-1".equals(request.getSort()) ) {
                selectItems(request, allProductResponse, "desc");
                return allProductResponse;
            }
            selectItems(request, allProductResponse, null);
            return allProductResponse;
        } catch (Exception e) {
            allProductResponse.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
            allProductResponse.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
        }
        return null;
    }

    private void selectItems(AllProductRequest request, AllProductResponse allProductResponse, String sort) {
        List<Item> items = itemMapper.selectItemFront(null, "price", sort, request.getPriceGt(), request.getPriceLte());
        //获取分页信息
        PageInfo<Item> itemsPageInfo = new PageInfo<>(items);
        long total = itemsPageInfo.getTotal();
        //转化Dto对象
        List<ProductDto> productDtos = productConverter.items2Dto(items);
        //无情塞数据工具人，wuu~~~
        allProductResponse.setProductDtoList(productDtos);
        allProductResponse.setTotal(total);
        allProductResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
        allProductResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
    }


    @Override
    public RecommendResponse getRecommendGoods() {
        //开始不知道这里老师写好了，用自己写的，这个就不管了
        return null;
    }


}
